#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <stdio.h>

#include <fist.h>
#include <kcfs.h>

int
main(int argc, char *argv[])
{
  int fd, ret,i,flag=1;
  struct termios new_settings,init_settings;
  ssize_t num;
  struct _fist_ioctl_PASSPRSE val;
  struct _fist_ioctl_GETSTATE val1;
  struct _fist_ioctl_SETENA val2;
  char tkey[33],chiper,keybytes;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    exit(1);
  }
  fd = open(argv[1], O_RDONLY);
  
  if (fd < 0) {
    perror(argv[1]);
    exit(1);
  }
login: 
  printf("Enter passphrase: \n");
  memset(val.passphrase, 0, 32);
  tcgetattr(0,&init_settings);
  new_settings=init_settings;
  new_settings.c_lflag&=~ECHO;
  tcsetattr(0,TCSANOW,&new_settings);
  fgets(tkey, 33, stdin);
  memcpy(val.passphrase,tkey,32);
  ret = ioctl(fd, FIST_IOCTL_PASSPRSE, &val);
  if (ret < 0) {
    perror("ioctl");
  }
  ret = ioctl(fd, FIST_IOCTL_GETSTATE, &val1);
  switch(val1.flag[0]){
     case -1: printf("Sorry,passphrase input error,login again:\n"); tcsetattr(0,TCSANOW,&init_settings); goto login; break;
     case 1 : printf("Login successfully!\n"); goto out; break;
     case 2 : printf("The account you input it does not exit:\n"); goto inz; break; 
}
inz:
  printf("WARNING: Based on the record of the kcfs,it look like you have never mounted with this password before.\nThis could mean you have typed your password wrong.\n\n");
  printf("Would you like to proceed with the mount to creat another account(y/n)\n");
  if(getchar()=='n'){
         printf("Aborting mount:\n");
         goto out;
  }
  tcsetattr(0,TCSANOW,&init_settings);
  printf("Enter your chosen chiper:\n1) Blowfish: blocksize =8Bytes; min keysize =32; max keysize =448\n2) AES     : blocksize =16Bytes; min keysize =128; max keysize =256\n");
  memset(val2.ena, 0, 3);
  getchar();
  chiper=getchar();
  getchar();
  if(chiper=='1'){
  	memcpy(val2.ena,"blw",3);
  	printf("Your chosen chiper is: Blowfish\n");
inx:  	
        printf("Select key bytes:\n1) 128\n2) 256\n3) 448\n");
  	keybytes=getchar();
  	switch(keybytes)
  	{
  		case '1': val2.kcfs_key_bytes=16; printf("Attempting to mount with the following options:\nkcfs_chiper=Blowfish\nkcfs_key_bytes=128\n"); break;
  		case '2': val2.kcfs_key_bytes=32; printf("Attempting to mount with the following options:\nkcfs_chiper=Blowfish\nkcfs_key_bytes=256\n"); break;
                case '3': val2.kcfs_key_bytes=56; printf("Attempting to mount with the following options:\nkcfs_chiper=Blowfish\nkcfs_key_bytes=448\n"); break;
  		default:  
  			flag=0;
  	}
        if(!flag)
               {
               printf("Input error,once again:\n");
               getchar();
               flag=1;
               goto inx;
               }  		
  	             }
  else if(chiper=='2')
  {
  	memcpy(val2.ena,"aes",3);
  	printf("Your chosen chiper is: AES\n");
iny:  	
	  printf("Select key bytes:\n1) 128\n2) 192\n3) 256\n");
    keybytes=getchar();
  	switch(keybytes)
  	{
  		case '1': val2.kcfs_key_bytes=16; printf("Attempting to mount with the following options:\nkcfs_cihper=AES\nkcfs_key_bytes=128\n"); break;
  		case '2': val2.kcfs_key_bytes=24; printf("Attempting to mount with the following options:\nkcfs_cihper=AES\nkcfs_key_bytes=192\n"); break;
  		case '3': val2.kcfs_key_bytes=32; printf("Attempting to mount with the following options:\nkcfs_chiper=AES\nkcfs_key_bytes=256\n"); break;
  		default:  
  		          flag=0;
  	}
      	if(!flag)
                {
                printf("Input error,once again:\n");
                getchar();
                flag=1;
                goto iny;
                }
  }
  else{
       printf("Input error,once again:\n");
       goto inz;
      }
  ret = ioctl(fd, FIST_IOCTL_SETENA, &val2);
  if (ret < 0) {
    perror("ioctl");
  }	


out:
  tcsetattr(0,TCSANOW,&init_settings);
  close(fd);
  exit(ret);
}
